package com.leinao.core.schedule;

import com.leinao.core.pool.TableInfoPool;
import com.leinao.domain.TableInfo;
import com.leinao.domain.param.DeviceDataQueryParam;
import com.leinao.service.TdengingService;
import com.leinao.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 晚上20点定时任务拉取
 *
 * 今天晚上20点，拉取
 * 今天08:00 - 今天 20:00 的数据
 *
 * @author zhong.wang
 * @date 2025/1/15
 */
//@Component
@Slf4j
public class PmScheduleTask {
    @Resource
    private TdengingService tdengingService;


    // 每天下午8点
    @Scheduled(cron = "0 0 20 * * ?")
    public void dailyTasks() throws ParseException {
        log.info("Task executed at 8 PM.");
        // 执行你的业务逻辑

        // 执行你的业务逻辑
        String dbStartTime = "2024-12-10 08:00:00";
        String dbEndTime = "2024-12-10 20:00:00";

        Date dbStart = DateUtils.parseDate(dbStartTime,DateUtils.YYYY_MM_DD_HH_MM_SS);
        Date dbEnd = DateUtils.parseDate(dbEndTime,DateUtils.YYYY_MM_DD_HH_MM_SS);

        Date startTime = DateUtils.parseDate(getStartTime(),DateUtils.YYYY_MM_DD_HH_MM_SS);
        Date endTime = DateUtils.parseDate(getEndTime(),DateUtils.YYYY_MM_DD_HH_MM_SS);

        Long durationDays = getDurationDays(dbStart,startTime);

        List<TableInfo> tableInfos = TableInfoPool.getInstance().getTableInfos();

        for (TableInfo tableInfo : tableInfos) {
            DeviceDataQueryParam queryParam = new DeviceDataQueryParam();
            queryParam.setTableName(tableInfo.getTbname());
            queryParam.setStartTime(dbStartTime);
            queryParam.setEndTime(dbEndTime);
            queryParam.setTimeGroupInterval(durationDays+"d");

            tdengingService.insertData(queryParam);
        }
    }


    /**
     * 今天早晨8点
     * @return
     */
    private String getStartTime(){
        Date time = new Date();
        String currentDate = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,time);
        String timeStr = currentDate + " " + "08:00:00";
        return timeStr;
    }

    /**
     * 今天下午20点
     * @return
     */
    private String getEndTime(){
        Date time = new Date();
        String currentDate = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,time);
        String timeStr = currentDate + " " + "20:00:00";
        return timeStr;
    }

    /**
     * 计算差多少天
     * @param dbTime
     * @param currentTime
     * @return
     */
    private Long getDurationDays(Date dbTime,Date currentTime){
        // 使用 DateUtils.truncate 截断时间部分，以确保只比较日期
        Date truncatedDbTime = DateUtils.truncate(dbTime, java.util.Calendar.DAY_OF_MONTH);
        Date truncatedCurrentTime = DateUtils.truncate(currentTime, java.util.Calendar.DAY_OF_MONTH);

        // 计算两个日期之间的毫秒数差异，然后转换为天数
        long diffInMillis = Math.abs(truncatedCurrentTime.getTime() - truncatedDbTime.getTime());
        long diffInDays = TimeUnit.MILLISECONDS.toDays(diffInMillis);

        return diffInDays;
    }
}
